package com.rc.saas.tenant.controller.ugs;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.utils.BeanUtil;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.model.ugs.TenantUgsLevelGrowing;
import com.rc.saas.tenant.model.ugs.TenantUgsLevelType;
import com.rc.saas.tenant.search.ugs.LevelDiscountRecordSearch;
import com.rc.saas.tenant.search.ugs.LevelDiscountStationSearch;
import com.rc.saas.tenant.service.elec.ElecStationService;
import com.rc.saas.tenant.service.ugs.TenantUgsLevelDiscountRecordService;
import com.rc.saas.tenant.service.ugs.TenantUgsLevelDiscountStationService;
import com.rc.saas.tenant.service.ugs.TenantUgsLevelGrowingService;
import com.rc.saas.tenant.service.ugs.TenantUgsLevelTypeService;
import com.rc.saas.tenant.shiro.ShiroUtils;
import com.rc.saas.tenant.vo.ugs.LevelDiscountStationResultVo;
import com.rc.saas.tenant.vo.ugs.TenantUgsLevelDiscountRecordResultVo;
import com.rc.saas.tenant.vo.ugs.UgsLevelDiscountStationResultVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author frog
 * @date 2021/4/2
 */
@Controller
@RequestMapping("/tenantUgsLevelType")
public class TenantUgsLevelTypeController {
    private static Logger logger = LogManager.getLogger(TenantUgsLevelTypeController.class);

    @Autowired
    private TenantUgsLevelTypeService tenantUgsLevelTypeService;

    @Autowired
    private TenantUgsLevelGrowingService tenantUgsLevelGrowingService;

    @Autowired
    private TenantUgsLevelDiscountStationService tenantUgsLevelDiscountStationService;

    @Autowired
    private TenantUgsLevelDiscountRecordService tenantUgsLevelDiscountRecordService;

    @Autowired
    private ElecStationService elecStationService;

    /**
     * 跳转用户等级页面
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String tenantUgsLevelTypePage() {
        return "/templates/ugs/tenantUgsLevelType";
    }

    /**
     * 返回用户等级列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageTenantUgsLevelType", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageTenantUgsLevelType(BaseSearch search) {
        PageInfo<TenantUgsLevelType> pageInfo = tenantUgsLevelTypeService.listPageTenantUgsLevelType(search);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转编辑用户等级
     *
     * @return
     */
    @RequestMapping(value = "/editTenantUgsLevelType/{ugsLevelTypeId}", method = RequestMethod.GET)
    public String editTenantUgsLevelType(@PathVariable("ugsLevelTypeId") Long ugsLevelTypeId, Model model) {
        TenantUgsLevelType tenantUgsLevelType = tenantUgsLevelTypeService.selectByPrimaryKey(ugsLevelTypeId);
        model.addAttribute("tenantUgsLevelType", tenantUgsLevelType);
        return "/templates/ugs/editTenantUgsLevelType";
    }


    /**
     * 保存/编辑用户等级
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateTenantUgsLevelType", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateTenantUgsLevelType(TenantUgsLevelType tenantUgsLevelType) {
        if (!BeanUtil.isValid(tenantUgsLevelType.getLevelTypeName(), tenantUgsLevelType.getExpireDay(),
                tenantUgsLevelType.getMaxValue(), tenantUgsLevelType.getKeepValue(),
                tenantUgsLevelType.getDiscountPercent())) {
            return TResponse.FAIL("请输入正确的参数");
        }
        try {
            if (tenantUgsLevelType.getUgsLevelTypeId() != null) {
                tenantUgsLevelTypeService.updateByPrimaryKeySelective(tenantUgsLevelType);
            } else {
                tenantUgsLevelTypeService.insertSelective(tenantUgsLevelType);
            }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存用户等级失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 跳转经验规则页面
     *
     * @return
     */
    @RequestMapping(value = "/tenantUgsLevelGrowing", method = RequestMethod.GET)
    public String tenantUgsLevelGrowing() {
        return "/templates/ugs/tenantUgsLevelGrowing";
    }

    /**
     * 返回经验规则列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageTenantUgsLevelGrowing", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageTenantUgsLevelGrowing(BaseSearch search) {
        PageInfo<TenantUgsLevelGrowing> pageInfo = tenantUgsLevelGrowingService.listPageTenantUgsLevelGrowing(search);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 跳转编辑经验规则
     *
     * @return
     */
    @RequestMapping(value = "/editTenantUgsLevelGrowing/{levelGrowingId}", method = RequestMethod.GET)
    public String editTenantUgsLevelGrowing(@PathVariable("levelGrowingId") Long levelGrowingId, Model model) {
        TenantUgsLevelGrowing tenantUgsLevelGrowing = tenantUgsLevelGrowingService.selectByPrimaryKey(levelGrowingId);
        model.addAttribute("tenantUgsLevelGrowing", tenantUgsLevelGrowing);
        return "/templates/ugs/editTenantUgsLevelGrowing";
    }


    /**
     * 保存/编辑经验规则
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateTenantUgsLevelGrowing", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateTenantUgsLevelGrowing(TenantUgsLevelGrowing tenantUgsLevelGrowing) {
        if (!BeanUtil.isValid(tenantUgsLevelGrowing.getGrowingTypeName(),
                tenantUgsLevelGrowing.getExperienceValue()
              //  ,
              //  tenantUgsLevelGrowing.getCycle(),
              //  tenantUgsLevelGrowing.getConditionValue()
        )) {
            return TResponse.FAIL("请输入正确的参数");
        }
        try {
            if (tenantUgsLevelGrowing.getLevelGrowingId() != null) {
                tenantUgsLevelGrowingService.updateByPrimaryKeySelective(tenantUgsLevelGrowing);
            } else {
                tenantUgsLevelGrowingService.insertSelective(tenantUgsLevelGrowing);
            }
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存经验规则失败", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 更改经验规则状态
     *
     * @return
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public TResponse updateStatus(Long levelGrowingId) {
        try {
            TResponse tResponse = tenantUgsLevelGrowingService.updateStatus(levelGrowingId);
            return tResponse;
        } catch (Exception e) {
            logger.error("更改经验规则状态", e);
            return TResponse.FAIL("操作失败");
        }
    }


    /**
     * 跳转会员等级折扣设置页面
     *
     * @return
     */
    @RequestMapping(value = "/levelDiscountStation", method = RequestMethod.GET)
    public String levelDiscountStation() {
        return "/templates/ugs/levelDiscountStation";
    }

    /**
     * 返回会员等级折扣列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageLevelDiscountStation", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageLevelDiscountStation(LevelDiscountStationSearch levelDiscountStationSearch) {
        levelDiscountStationSearch.setDiscountType("0");
        PageInfo<LevelDiscountStationResultVo> pageInfo = tenantUgsLevelDiscountStationService.listPageLevelDiscountStation(levelDiscountStationSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 跳转增加桩群折扣页面
     *
     * @return
     */
    @RequestMapping(value = "/editStationDiscount/{elecStationId}", method = RequestMethod.GET)
    public String editStationDiscount(@PathVariable("elecStationId") Long elecStationId, Model model) {
        ElecStation elecStation = elecStationService.selectByPrimaryKey(elecStationId);
        model.addAttribute("elecStation", elecStation);
        UgsLevelDiscountStationResultVo ugsLevelDiscountStationResultVo = tenantUgsLevelDiscountStationService.selectDiscountByType("0", elecStationId);
        model.addAttribute("ugsLevelDiscountStationResultVo", ugsLevelDiscountStationResultVo);
        return "/templates/ugs/editStationDiscount";
    }

    /**
     * 跳转会员修改记录页面
     *
     * @return
     */
    @RequestMapping(value = "/modifyRecord/{elecStationId}", method = RequestMethod.GET)
    public String modifyRecord(@PathVariable("elecStationId") Long elecStationId, Model model) {
        model.addAttribute("elecStationId", elecStationId);
        return "/templates/ugs/modifyRecord";
    }


    /**
     * 返回会员修改记录列表数据
     *
     * @return
     */
    @RequestMapping(value = "/listPageModifyRecord", method = RequestMethod.GET)
    @ResponseBody
    public WebPageInfo listPageModifyRecord(LevelDiscountRecordSearch levelDiscountRecordSearch) {
        levelDiscountRecordSearch.setDiscountType("0");
        PageInfo<TenantUgsLevelDiscountRecordResultVo> pageInfo = tenantUgsLevelDiscountRecordService.listPageModifyRecord(levelDiscountRecordSearch);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }


    /**
     * 保存/编辑桩群折扣
     *
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateStationDiscount", method = RequestMethod.POST)
    @ResponseBody
    public TResponse saveOrUpdateStationDiscount(UgsLevelDiscountStationResultVo ugsLevelDiscountStationResultVo) {
        if ("0".equals(ugsLevelDiscountStationResultVo.getStatus())) {
            if (!BeanUtil.isValid(ugsLevelDiscountStationResultVo.getL1(), ugsLevelDiscountStationResultVo.getL2(),
                    ugsLevelDiscountStationResultVo.getL3(), ugsLevelDiscountStationResultVo.getL4(), ugsLevelDiscountStationResultVo.getL5(),
                    ugsLevelDiscountStationResultVo.getBeginTime(), ugsLevelDiscountStationResultVo.getEndTime())) {
                return TResponse.FAIL("请输入正确的参数");
            }
        }

        TenantUser user = ShiroUtils.getSessionUser();

        ugsLevelDiscountStationResultVo.setDiscountType("0");
        ugsLevelDiscountStationResultVo.setTenantCode(user.getTenantCode());
        tenantUgsLevelDiscountStationService.saveOrUpdateStationDiscount(ugsLevelDiscountStationResultVo);
        try {
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("保存桩群折扣失败", e);
            return TResponse.FAIL("操作失败");
        }
    }

}
